package com.chuanke.ckfamily.action;

import com.chuanke.ckfamily.action.common.ApiCheckerFilter;
import com.chuanke.ckfamily.action.common.model.Header;
import com.chuanke.ckfamily.dao.common.model.Equipment;
import com.chuanke.ckfamily.dao.user.model.User;
import com.chuanke.ckfamily.service.common.CacheService;
import com.chuanke.ckfamily.service.common.EquipmentService;
import com.chuanke.ckfamily.service.common.model.UserToken;
import com.chuanke.ckfamily.service.exception.CFException;
import com.chuanke.ckfamily.service.user.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private CacheService cacheService;

	@Autowired
	private UserService userService;

	@Autowired
	private EquipmentService equipmentService;

	protected User getUserByToken(HttpServletRequest request) throws CFException {
		return userService.findById(getUidByToken(request));
	}

	protected Long getUidByToken(HttpServletRequest request){
//		return Long.parseLong(request.getParameter("uid"));
		UserToken userToken = getUserToken(request);
		if(userToken != null){
			return userToken.getId();

		} else {
			return null;

		}
	}

	protected UserToken getUserToken(HttpServletRequest request) {
		String token = request.getHeader(ApiCheckerFilter.HEAD_TOKEN);
		return (UserToken)cacheService.get(UserToken.KEY + token);
	}

	/**
	 * 获取请求头
	 * @param request
	 * @return
	 */
	public Header getHeader(HttpServletRequest request){
		String token=request.getHeader("token");
		String sign=request.getHeader("sign");
		String imei=request.getHeader("imei");
		String os=request.getHeader("os");
		String version=request.getHeader("version");
		String agent=request.getHeader("agent");
		Header header=new Header();
		header.setToken(token);
		header.setSign(sign);
		header.setImei(imei);
		header.setOs(os);
		header.setVersion(version);
		header.setAgent(agent);
		return header;
	}

	public void refreshVersion(HttpServletRequest request, String uid){
		try {
			Header header = getHeader(request);
			if(uid==null){
				return ;
			}

			logger.debug("refreshVersion=========================>uid={}, header={}", uid, header);
			cacheService.delete("version_"+uid);
			if(header.getAgent()!=null){
				cacheService.set("version_"+uid, "new");

			}else{
				cacheService.set("version_"+uid, "old");

			}

		} catch (Exception e) {
			e.printStackTrace();

		}	
	}
	
	public boolean getVersion(Integer uid){
		try {
			String ver=(String) cacheService.get("version_"+uid);
			if(ver!=null && "new".equals(ver)){
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private boolean isTestEquipment(String imie) {
		Equipment equipment = equipmentService.getEqupment(imie);
		return equipment != null;
	}
	
	protected boolean isTestEquipment(HttpServletRequest request) {
		String imie = request.getHeader(ApiCheckerFilter.HEAD_IMEI);
		Equipment equipment = equipmentService.getEqupment(imie);
		return equipment != null;
	}
	
	protected String getUserAgent(HttpServletRequest request) {
		return  request.getHeader("User-Agent");
	}
	
	
	protected boolean isWx(HttpServletRequest request) {
		String ua = getUserAgent(request);
		return ua != null && ua.indexOf("MicroMessenger") != -1;
	}
	protected boolean isMobileQQ(HttpServletRequest request) {
		String ua = getUserAgent(request);
		return ua!= null && (ua.indexOf("Mobile")!=-1 && ua.indexOf("QQ/")!=-1);
	}
	protected boolean isIos(HttpServletRequest request) {
		String ua = getUserAgent(request);
		return ua != null && (ua.indexOf("Mac") != -1 || ua.indexOf("iPhone") != -1);
	}
	protected boolean isAndroid(HttpServletRequest request) {
		String ua = getUserAgent(request);
		return ua != null && (ua.indexOf("Android") != -1 || ua.indexOf("Linux") != -1);
	}

	
}
